<template>
	<om-formsub-outer :index="index" :checkedit="checkedit">
		<om-autoform ref="autoform" :edit="edit" :checkedit="checkedit" :initConfigData="autoformdata" :initValue="initValue"></om-autoform>
	</om-formsub-outer>
</template>

<script>
	import {
		buildSubFormData,
		subOnLoad, 
		changeEditor,
		configSelectData 
	} from '../../../static/js/formUtil.js';
	import {
		ignoreHtml,
		ptFloat,
		minusF,
		getDateStr
	} from '@/static/js/common.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildSubFormData({
				indexId:'',
				areaCacheKey:'obj-ss-tsalesForecastAnalysis-title_data-area-cache',
				placeCacheKey:'obj-ss-tsalesForecastAnalysis-title_data-place-cache',
				customerCacheKey:'obj-ss-tsalesForecastAnalysis-title_data-customer-cache',
				productCategoryCacheKey:'obj-ss-tsalesForecastAnalysis-title_data-productCategory-cache'
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId'])
		},
		methods:{
			onSelectPlace(item){
				let _that = this
				_that.$refs.autoform.formdata.placeId = item.id
				_that.$refs.autoform.formdata.placeName = item.fullName
				_that.$refs.autoform.changeEditor("title_data","customerCode",'type','linkbox')
				_that.$refs.autoform.changeEditor("title_data","customerCode",{
					link:'../../public/pageSearch/pageSearch',
					"linkParam":{
						other:JSON.stringify({
							userId: _that.userId,
							regionsId:item.id
						}),
						url:'customerShow/getCustomerPage',
						initload:true,
						namefield:'customerName',
						searchfield:'q',
						cachename:_that.customerCacheKey
					}
				})
			},
			initload(){
				let _that = this
				let arr = [
					_that.om.request(
						'indexDict/getCategoryList',
						{
							indexId:_that.indexId
						}
					),
					_that.om.request(
						'tsysdict/getCombobox',
						{
							code:'39053'
						}
					)
					
				]
				Promise.all(arr).then(res=>{
					if(!_that.indexId){
						_that.$refs.autoform.clear("title_data","busRange")
						_that.$refs.autoform.changeEditor("title_data","busRange","type",'labelbox')
					}else{
						_that.$refs.autoform.changeEditor("title_data","busRange","data",configSelectData(res[0],'value','name'))
						_that.$refs.autoform.changeEditor("title_data","busRange","type",'selectbox')
						_that.$refs.autoform.initSelectIndex("title_data","busRange")
					}
					_that.$refs.autoform.changeEditor("title_data","productCateId","data",configSelectData(res[1],'id','dicname'))
					_that.$refs.autoform.initSelectIndex("title_data","productCateId")
				})
			},
			
		},
		onLoad(e) {
			let _that = this
			subOnLoad(_that, e, function() {
				if (e.configDataCacheKey) {
					_that.indexId  = e.indexId
					uni.getStorage({
						key: e.configDataCacheKey,
						success: function(res) {
							//if (res.data && res.data instanceof Array) {
							if(res.data && Object.prototype.toString.call(res.data)==='[object Array]'){
								_that.autoformdata = res.data
							}
							Object.assign(_that.initValue, _that.formdata)
							_that.autoformdata[0].order = _that.order
							changeEditor("title_data","areaId",{
								link:'../../public/search/search',
								"linkParam":{
									other:JSON.stringify(),
									url:'regions/getCountryList',
									initload:true,
									namefield:'dicname',
									cachename:_that.areaCacheKey
								}
							},null,_that.autoformdata)
							changeEditor('title_data', 'placeId','type','areabox',_that.autoformdata)
							changeEditor('title_data', 'placeId', {
								link: '../../public/pageSearch/pageSearch',
								"linkParam":{
									other: JSON.stringify({
										classifyCode: '37056'
									}),
									searchfield: 'q',
									namefield: urlUtil.addressUrl().namefield,
									url: urlUtil.addressUrl().url,
									fullName:true,
									cachename: _that.placeCacheKey,
									placeholder: '请输入地址'
								}
							},null,_that.autoformdata)
							_that.initload()
							if(_that.formdata.placeId){
								changeEditor("title_data","customerCode",'type','linkbox',_that.autoformdata)
								changeEditor("title_data","customerCode",{
									link:'../../pageSearch/pageSearch',
									"linkParam":{
										other:JSON.stringify({
											userId: _that.userId,
											regionsId:_that.formdata.placeId
										}),
										url:'customerShow/getCustomerPage',
										initload:true,
										namefield:'customerName',
										cachename:_that.customerCacheKey
									}
								},null,_that.autoformdata)
							}else{
								changeEditor("title_data","customerCode",'type','labelbox',_that.autoformdata)
							}
						}
					})
				}
			})
		},
		onShow() {
			let _that = this
			if (_that.$refs.autoform) {
				uni.getStorage({
					key:_that.areaCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.areaId = d.id
							formdata.areaName = d.dicname
							uni.removeStorage({
								key:_that.areaCacheKey
							})
						}
					}
				})
				uni.getStorage({
					key:_that.placeCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							_that.$refs.autoform.$refs.areapop.close()
							formdata.placeId = d.id
							formdata.placeName = d.fullName
							_that.$refs.autoform.changeEditor("title_data","customerCode",'type','linkbox')
							_that.$refs.autoform.changeEditor("title_data","customerCode",{
								link:'../../public/pageSearch/pageSearch',
								"linkParam":{
									other:JSON.stringify({
										userId: _that.userId,
										regionsId:d.id
									}),
									url:'customerShow/getCustomerPage',
									initload:true,
									namefield:'customerName',
									searchfield:'q',
									cachename:_that.customerCacheKey
								}
							})
							uni.removeStorage({
								key:_that.placeCacheKey
							})
						}
					}
				})
				uni.getStorage({
					key:_that.customerCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.customerCode = d.customerCode
							formdata.customerId = d.customerId
							formdata.customerShortName = d.customerShortName
							formdata.customerName = d.customerName
							uni.removeStorage({
								key:_that.customerCacheKey
							})
						}
					}
				})
				uni.getStorage({
					key:_that.productCategoryCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.productCategory = d.map(e=>e.typeName).join(",")
							formdata.productCategoryIdStr = d.map(e=>e.id).join(",")
							_that.$refs.autoform.changeEditor("title_data","productCategoryIdStr",{
								initSelect:d
							})
							uni.removeStorage({
								key:_that.productCategoryCacheKey
							})
						}
					}
				})
			}
		}
	}
</script>

<style>
</style>
